﻿using System;
using System.Configuration;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Client
{
    /// <summary>
    /// 注册
    /// </summary>
    /// <param name="UserName"></param>
    /// <param name="RealName"></param>
    /// <param name="Password"></param>
    /// <param name="Address"></param>
    /// <param name="phone"></param>
    /// <param name="Email"></param>
    /// <param name="RoleID"></param>
    /// <param name="Remark"></param>
    /// <returns>0数据库访问错误；-1表示存在用户；其它表示成功</returns>
    public int Register(string UserName, string RealName, string Password,
        string Address, string Phone, string Email)
    {
        SqlParameter[] param = new SqlParameter[6];
        param[0] = new SqlParameter("@userName", UserName);
        param[1] = new SqlParameter("@realName", RealName);
        param[2] = new SqlParameter("@password", Password);
        param[3] = new SqlParameter("@address", Address);
        param[4] = new SqlParameter("@phone", Phone);
        param[5] = new SqlParameter("@email", Email);
        ExecuteProcedure exec = new ExecuteProcedure();
        int clientID = 0;
        string ProcName = "Pr_Register";
        exec.run(out clientID, ProcName ,param);
        return clientID;
    }
    /// <summary>
    /// 认证用户
    /// </summary>
    /// <param name="username"></param>
    /// <param name="password"></param>
    /// <returns>-1表示认证不成功；0表示访问数据库失败；1表示认证成功</returns>
    public int IdentifyUser(string username, string password)
    {
        DataModelDataContext data = new DataModelDataContext();
        var result = data.Pr_IdentifyClient(username, password);
        DataTable table = new DataTable();
        table = result.CopyToDataTable();
        if (table != null)
        {
            
            try
            {
                int tp = 0;
                foreach(DataRow r in table.Rows)
                    foreach (DataColumn c in table.Columns) 
                    {
                        tp = Convert.ToInt32(r[c].ToString());
                    }
                return tp;
            }
            catch(Exception e)
            {
                return -1;
            }
        }
        else
            return 0;
    }
    /// <summary>
    /// 修改用户信息
    /// </summary>
    /// <param name="userID"></param>
    /// <param name="RealName"></param>
    /// <param name="Address"></param>
    /// <param name="phone"></param>
    /// <param name="Email"></param>
    /// <param name="Remark"></param>
    /// <returns></returns>
    public bool UpdateUserInfo(int userID, string userName, string RealName,
        string Address, string phone, string Email)
    {
        DataModelDataContext data = new DataModelDataContext();
        data.Pr_UpdateClient(userID, RealName, Address, phone, Email);
        return true;
    }
    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="userID"></param>
    /// <param name="Password"></param>
    /// <returns></returns>
    public void ChangePassword(int userID, string Password)
    {
        DataModelDataContext data = new DataModelDataContext();
        data.Pr_ChangePassword(userID, Password);
    }

    /// <summary>
    /// 获得所有的用户
    /// </summary>
    /// <returns></returns>
    public DataTable GetUser()
    {
        DataModelDataContext data = new DataModelDataContext();
        var result = data.Pr_GetAllClients();
        DataTable table = new DataTable();
        table = result.CopyToDataTable();
        return table;
    }
    /// <summary>
    /// 获得某个用户的信息
    /// </summary>
    /// <param name="userID"></param>
    /// <returns></returns>
    public void GetUser(int userID)
    {
        DataModelDataContext data = new DataModelDataContext();
        var result = data.Pr_GetClientByID(userID);
        DataTable table = new DataTable();
        table = result.CopyToDataTable();
        this.clientID = (int)table.Rows[0]["clientID"];
        this.userName = (string)table.Rows[0]["userName"];
        this.realName = (string)table.Rows[0]["realName"];
        this.address  = (string)table.Rows[0]["address"];
        this.email = (string)table.Rows[0]["email"];
        this.phone = (string)table.Rows[0]["phone"];
        this.roleID = (int)table.Rows[0]["roleID"];
        this.regTime = (DateTime)table.Rows[0]["regTime"];
        this.lastLog = (table.Rows[0]["lastLog"] is DBNull) ? this.regTime :((DateTime)table.Rows[0]["lastLog"]);
    }

    /// <summary>
    /// 测试某用户名是否可用
    /// </summary>
    /// <param name="userID"></param>
    /// <returns></returns>
    public int TestRegister(string UserName)
    {
        SqlParameter[] param = new SqlParameter[2];
        param[0] = new SqlParameter("@userName", UserName);
        ExecuteProcedure exec = new ExecuteProcedure();
        int flag = 0;
        string ProcName = "TestRegister";
        exec.run(out flag, ProcName, param);
        return clientID;
    }
}